/*
 * Copyright (c) 2009. Silenus Consultoria, S.L.
 */
package es.silenus.detecta.model;

import java.sql.Date;

import java.util.HashSet;
import java.util.Set;


/**
 * A survey form answer (filled-form) header that contains common and identity fields. Please be aware that this
 * class holds several form questions and a reference to the rest of the form questions. Persistent class, read-write.
 *
 * @author <a href="mailto:malonso@silenus-consultoria.es">Mariano Alonso</a>
 *
 * @since 22-mar-2009 11:04:04
 */
public class SurveyAnswerHeader implements java.io.Serializable {
	/**
	 * The identifier.
	 */
	private Long id;

	/**
	 * The creation date, when the user filled the form.
	 */
	private Date creationDate;

	/**
	 * The import date, when the form was imported in this system.
	 */
	private Date importDate;

	/**
	 * The training action.
	 */
	private TrainingAction action;

	/**
	 * The identifier of the person or company which filled the survey.
	 */
	private String entityId;

	/**
	 * The internet connection flag.
	 */
	private Integer internetConnection;

	/**
	 * The council where the form was filled, normally during a training action.
	 */
	private Council council;

	/**
	 * The survey this answer header belongs to.
	 */
	private Survey survey;

	/**
	 * The answer origin.
	 */
	private AnswerOrigin origin;

	/**
	 * The unit answers, part of this filled survey.
	 */
	private Set<SurveyUnitAnswer> unitAnswers;

/**
   * Constructor.
   */
	public SurveyAnswerHeader() {
		unitAnswers = new HashSet<SurveyUnitAnswer>();
		importDate = new Date(System.currentTimeMillis());
		creationDate = new Date(System.currentTimeMillis());
		origin = AnswerOrigin.WEB;
	}

	/**
	 * Retrieves the identifier.
	 *
	 * @return the identifier.
	 */
	public Long getId() {
		return this.id;
	}

	/**
	 * Sets the identifier.
	 *
	 * @param id the identifier.
	 */
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * Retrieves the creation date.
	 *
	 * @return the creation date.
	 */
	public Date getCreationDate() {
		return creationDate;
	}

	/**
	 * Sets the creation date.
	 *
	 * @param creationDate the creation date.
	 */
	public void setCreationDate(final Date creationDate) {
		this.creationDate = creationDate;
	}

	/**
	 * Retrieves the import date.
	 *
	 * @return the import date.
	 */
	public Date getImportDate() {
		return importDate;
	}

	/**
	 * Sets the import date.
	 *
	 * @param importDate the import date.
	 */
	public void setImportDate(final Date importDate) {
		this.importDate = importDate;
	}

	/**
	 * Retrieves the internet connection flag.
	 *
	 * @return the internet connection flag.
	 */
	public Integer getInternetConnection() {
		return internetConnection;
	}

	/**
	 * Sets the internet connection flag.
	 *
	 * @param internetConnection the internet connection flag.
	 */
	public void setInternetConnection(final Integer internetConnection) {
		this.internetConnection = internetConnection;
	}

	/**
	 * Retrieves the council.
	 *
	 * @return the council.
	 */
	public Council getCouncil() {
		return this.council;
	}

	/**
	 * Sets the council.
	 *
	 * @param council the council.
	 */
	public void setCouncil(final Council council) {
		this.council = council;
	}

	/**
	 * Retrieves the survey.
	 *
	 * @return the survey.
	 */
	public Survey getSurvey() {
		return this.survey;
	}

	/**
	 * Sets the survey.
	 *
	 * @param survey the survey.
	 */
	public void setSurvey(final Survey survey) {
		this.survey = survey;
	}

	/**
	 * Retrieves the origin.
	 *
	 * @return the origin.
	 */
	public AnswerOrigin getOrigin() {
		return origin;
	}

	/**
	 * Sets the origin.
	 *
	 * @param origin the origin.
	 */
	public void setOrigin(final AnswerOrigin origin) {
		this.origin = origin;
	}

	/**
	 * Retrieves the training action.
	 *
	 * @return the training action.
	 */
	public TrainingAction getAction() {
		return action;
	}

	/**
	 * Sets the training action.
	 *
	 * @param action the training action.
	 */
	public void setAction(final TrainingAction action) {
		this.action = action;
	}

	/**
	 * Retrieves the entity id.
	 *
	 * @return the entity id.
	 */
	public String getEntityId() {
		return this.entityId;
	}

	/**
	 * Sets the entity id.
	 *
	 * @param entityId the entity id.
	 */
	public void setEntityId(final String entityId) {
		this.entityId = entityId;
	}

	/**
	 * Retrieves the unit answers.
	 *
	 * @return the unit answers.
	 */
	public Set<SurveyUnitAnswer> getUnitAnswers() {
		return this.unitAnswers;
	}

	/**
	 * Sets the unit answers.
	 *
	 * @param unitAnswers the unit answers.
	 */
	public void setUnitAnswers(final Set<SurveyUnitAnswer> unitAnswers) {
		this.unitAnswers = unitAnswers;
	}

	/**
	 * Add an answer.
	 *
	 * @param answer the answer.
	 */
	public void add(final SurveyUnitAnswer answer) {
		answer.setHeader(this);
		getUnitAnswers().add(answer);
	}

	/**
	 * Remove an answer.
	 *
	 * @param answer the answer.
	 */
	public void remove(final SurveyUnitAnswer answer) {
		getUnitAnswers().remove(answer);
		answer.setHeader(null);
	}

	/**
	 * Checks if objects are equal.
	 *
	 * @param o other object.
	 *
	 * @return true if equal, false otherwise.
	 */
	@Override
	public boolean equals(Object o) {
		if(this == o) {
			return true;
		}

		if((o == null) || (getClass() != o.getClass())) {
			return false;
		}

		SurveyAnswerHeader that = (SurveyAnswerHeader)o;

		if((getId() != null) ? (!getId().equals(that.getId())) : (that.getId() != null)) {
			return false;
		}

		return true;
	}

	/**
	 * Calculates the hash of this instance.
	 *
	 * @return the hash code.
	 */
	@Override
	public int hashCode() {
		return (getId() != null) ? getId().hashCode() : 0;
	}

	/**
	 * Stringifies this instance.
	 *
	 * @return the string representation.
	 */
	@Override
	public String toString() {
		final StringBuilder sb = new StringBuilder();

		sb.append("SurveyAnswerHeader");
		sb.append("{id=").append(getId());
		sb.append(", entityId='").append(getEntityId()).append('\'');
		sb.append('}');

		return sb.toString();
	}
}
